/*
 * YODANESS.cxx
 *
 *  Created on: 12:38:35 AM Nov 1, 2012
 *      Author: Pallab
 * "I have not failed, I have just found 10000 ways that won't work.
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait(double seconds) {
	double endtime = clock() + (seconds * CLOCKS_PER_SEC);
	while (clock() < endtime) {
		;
	}
}
#define T int
inline T fastIn() {
	register char c = 0;
	register T a = 0;
	bool neg = false;
	while (c < 33)
		c = getchar();
	while (c > 33) {
		if (c == '-') {
			neg = true;
		} else {
			a = (a * 10) + (c - '0');
		}
		c = getchar();
	}
	return neg ? -a : a;
}
inline void readword(char *buffer) {
	int c, cnt = 0;
	c = getchar();
	while (c >= 0 && c <= ' ') {
		c = getchar();
	}
	if (c < 0) {
		buffer[cnt] = 0;
		return;
	}
	while (c > ' ') {
		buffer[cnt++] = c;
		c = getchar();
	}
	buffer[cnt] = 0;
}
struct mystr {
	char *buff;
	mystr() {
	}
	mystr(char *str) {
		this->buff = (char *) strdup(str);
	}
	bool operator<(const mystr &r) const {
		return strcmp(this->buff, r.buff) < 0 ? true : false;
	}
};
map<mystr, int> ids;
const int MX = 30001;
int words_count;
char words[22];
char correct_words[MX][22];
inline void read() {
	ids.clear();
	words_count = fastIn();
	fo(i,words_count)
		readword(words),
		ids[ mystr(words) ]=i;
	fo(i,words_count)
		readword(correct_words[i]);

	/*fo(i,words_count)
	 cout<<words[i]<<"_"<<correct_words[i]<<"\n";
	 puts("-------");*/
}

namespace segtree {
int t[4 * MX];
void build(int v, int tl, int tr) {
	if (tl == tr)
		t[v] = 1;
	else {
		int tm = (tl + tr) >> 1, v1 = v << 1, v2 = (v << 1) + 1;
		build(v1, tl, tm);
		build(v2, tm + 1, tr);
		t[v] = t[v1] + t[v2];
	}
}
int sum(int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t[v];
	int tm = (tl + tr) >> 1, v1 = v << 1, v2 = (v << 1) + 1;
	return sum(v1, tl, tm, l, min(r, tm))
			+ sum(v2, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t[v] = new_val;
	else {
		int tm = (tl + tr) >> 1, v1 = v << 1, v2 = (v << 1) + 1;
		if (pos <= tm)
			update(v1, tl, tm, pos, new_val);
		else
			update(v2, tm + 1, tr, pos, new_val);
		t[v] = t[v1] + t[v2];
	}
}
}
inline void proc() {
	segtree::build(1, 0, words_count - 1);
	Int sum = 0;
	fo(i,words_count)
	{
		int real_indx = ids[correct_words[i]];
		int get = segtree::sum(1, 0, words_count - 1, 0, real_indx);
		segtree::update(1, 0, words_count - 1, real_indx, 0);
		//cout << correct_words[i] << ' ' << get << '\n';
		sum += max(0, get - 1);
	}
	cout << sum << '\n';
}
int main() {
	int kase = 1;
#if defined( xerxes_pc )
	if (!freopen("in1", "r", stdin))
		puts("error opening file in "), assert(0);
	kase = 1;
#endif
	kase = fastIn();
	fo(i,kase)
	{
		read();
		proc();
	}
	return 0;
}
